﻿using Company.Database.Entities;
using Microsoft.EntityFrameworkCore;

namespace Company.Database.Sqlite
{
    public class SqliteDbContext : DbContext
    {
        public DbSet<UserEntity> Users { get; set; }
        public DbSet<AlarmEntity> Alarms { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            //设置Sqlite数据库的路径
            string databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), this.GetType().Name, "database2.db");
            //创建目录
            Directory.CreateDirectory(Path.GetDirectoryName(databasePath));
            //使用数据库
            optionsBuilder.UseSqlite($"Data Source={databasePath}");
            File.WriteAllText("database.txt", databasePath);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            Database.Migrate();//迁移数据库
        }
    }
}
